package com.wang.springbootinit.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.springbootinit.mapper.ActivityMapper;
import com.wang.springbootinit.mapper.EvaluationMapper;
import com.wang.springbootinit.model.entity.Activity;
import com.wang.springbootinit.model.entity.Evaluation;
import com.wang.springbootinit.model.entity.Grade;
import com.wang.springbootinit.model.entity.Student;
import com.wang.springbootinit.model.vo.StatsVO;
import com.wang.springbootinit.service.ActivityService;
import com.wang.springbootinit.service.EvaluationService;
import com.wang.springbootinit.service.GradeService;
import com.wang.springbootinit.service.StudentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toMap;

/**
 * 评分服务实现类
 */
@Service
public class EvaluationServiceImpl extends ServiceImpl<EvaluationMapper, Evaluation> implements EvaluationService {

    @Resource
    private EvaluationMapper evaluationMapper;

    @Resource
    private GradeService gradeService;

    @Resource
    private StudentService studentService;

    @Override
    @Transactional
    public Evaluation createEvaluation(Evaluation evaluation) {
        evaluation.setId(null);
        evaluation.setCreateTime(LocalDateTime.now());
        evaluation.setUpdateTime(LocalDateTime.now());
        evaluation.setIsDelete(0);
        evaluationMapper.insert(evaluation);
        return evaluation;
    }

    @Override
    @Transactional
    public Evaluation updateEvaluation(Evaluation evaluation) {
        evaluation.setUpdateTime(LocalDateTime.now());
        evaluationMapper.updateById(evaluation);
        return evaluation;
    }

    @Override
    @Transactional
    public boolean deleteEvaluation(Long id) {
        UpdateWrapper<Evaluation> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.set("isDelete",1);
        return update(updateWrapper);
    }

    @Override
    public Evaluation getEvaluation(Long id) {
        return evaluationMapper.selectById(id);
    }

    @Override
    public List<Evaluation> getEvaluationsByType(Integer type) {
        return evaluationMapper.getByType(type);
    }

    @Override
    public List<Evaluation> listValidEvaluations() {
        return evaluationMapper.listValidEvaluations();
    }

    @Override
    public List<String> getEvaluation() {
        LambdaQueryWrapper<Grade> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Grade::getSemester);
        queryWrapper.groupBy(Grade::getSemester);
        queryWrapper.orderByAsc(Grade::getSemester);
        List<Grade> list = gradeService.list(queryWrapper);
        if(CollUtil.isEmpty(list)){
            return new ArrayList<>();
        }
        return list.stream().map(Grade::getSemester).collect(Collectors.toList());
    }

    @Override
    public List<HashMap<String, String>> getEvaluationNameList() {
        LambdaQueryWrapper<Evaluation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Evaluation::getEvaluationName);
        queryWrapper.groupBy(Evaluation::getEvaluationName);
        queryWrapper.orderByDesc(Evaluation::getEvaluationName);
        List<Evaluation> list = list(queryWrapper);
        if(CollUtil.isEmpty(list)){
            return new ArrayList<>();
        }
        return list.stream().map(item -> {
            HashMap<String, String> map = new HashMap<>();
            map.put("label", item.getEvaluationName());
            map.put("value", item.getEvaluationName());
            return map;
        }).collect(Collectors.toList());
    }

    /**
     * 获取统计信息
     * @return
     */
    @Override
    public List<StatsVO> getStats() {
        List<Student> studentList = studentService.list();
        if(CollUtil.isEmpty(studentList)){
            return new ArrayList<>();
        }
        List<Long> studentIds = studentList.stream().map(Student::getId).collect(Collectors.toList());
        Map<Long, Student> studentMap = studentList.stream().collect(toMap(Student::getId, Function.identity()));

        LambdaQueryWrapper<Grade> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Grade::getStudentId,studentIds);
        List<Grade> gradeList = gradeService.list(queryWrapper);


        return null;
    }
} 